﻿Imports MySql.Data.MySqlClient
Imports System.Text.RegularExpressions
Imports System.Text

Public Class proveedores

    Dim connectionstring As String
    Dim commandtext As String
    Dim adapter As MySqlDataAdapter
    Dim table As DataTable

    Dim codPro, empPro, cat, nomPro, apePro, telPro, mailPro As String

 
    Private Sub proveedores_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connectionstring = "server=localhost;user id=root;password=root;database=zoo;"
        commandtext = "SELECT * FROM contactos"

        Try
            adapter = New MySqlDataAdapter(commandtext, connectionstring)
            table = New DataTable
            adapter.Fill(table)

            dgv_prov.DataSource = table
            dgv_prov.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None

            txt_apePro.DataBindings.Add("Text", table, "apecont")
            txt_codPro.DataBindings.Add("Text", table, "id")
            txt_dirPro.DataBindings.Add("Text", table, "direccion")
            txt_empPro.DataBindings.Add("Text", table, "nombre")
            txt_mailPro.DataBindings.Add("Text", table, "email")
            txt_nomPro.DataBindings.Add("Text", table, "nomcont")
            txt_telPro.DataBindings.Add("Text", table, "telefono")
            txt_webPro.DataBindings.Add("Text", table, "web")
            cbox_cat.DataBindings.Add("Text", table, "tipo")
            registro()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        'CARGA LAS CATEGORIAS DE PROVEEDORES
        Dim tipos() As String = {"Comida", "Piensos", "Complementos", "Jaulas", "Ropa", "Higiene", "Lechos"}
        For i = 0 To tipos.Length - 1
            cbox_cat.Items.Add(tipos(i))
        Next
    End Sub

    Private Sub proveedores_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Form1.Show()
    End Sub

    Private Sub txt_codPro_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_codPro.KeyPress
        'validarNumero(e)
    End Sub

    Private Sub txt_codPro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_codPro.Leave
        codPro = Integer.Parse(Trim(txt_codPro.Text))
    End Sub

    Private Sub txt_empPro_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_empPro.TextChanged
        empPro = Trim(txt_empPro.Text)
    End Sub

    Private Sub txt_webPro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_webPro.Leave
        validarWeb(txt_webPro.Text)
    End Sub

    Private Sub cbox_cat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbox_cat.SelectedIndexChanged
        cat = cbox_cat.SelectedItem
    End Sub

    Private Sub txt_nomPro_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nomPro.KeyPress
        'validarNombre(e)
    End Sub

    Private Sub txt_nomPro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_nomPro.Leave
        nomPro = Trim(txt_nomPro.Text)
    End Sub

    Private Sub txt_apePro_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_apePro.KeyPress
        'ValidarApellidos(txt_apePro)

    End Sub

    Private Sub txt_apePro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_apePro.Leave
        apePro = Trim(txt_apePro.Text)
    End Sub

    Private Sub txt_telPro_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_telPro.KeyPress
        'validarNumero(e)
    End Sub

    Private Sub txt_telPro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_telPro.Leave
        telPro = Integer.Parse(Trim(txt_telPro.Text))
    End Sub

    Private Sub txt_mailPro_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mailPro.Leave
        ErrorProvider1.SetError(txt_mailPro, Nothing)
        If Not (txt_mailPro.Text = Nothing) Then
            Try
                Dim mail As New System.Net.Mail.MailAddress(txt_mailPro.Text)
                mailPro = txt_mailPro.Text
            Catch ex As Exception
                ErrorProvider1.SetError(txt_mailPro, "Formato incorrecto")
                txt_mailPro.Focus()
            End Try
        End If
    End Sub

    Private Sub btn_inicio_Click(sender As System.Object, e As System.EventArgs) Handles btn_inicio.Click
        BindingContext(table).Position = 0
        registro()
    End Sub

    Private Sub btn_anterior_Click(sender As System.Object, e As System.EventArgs) Handles btn_anterior.Click
        BindingContext(table).Position -= 1
        registro()
    End Sub

    Private Sub btn_siguiente_Click(sender As System.Object, e As System.EventArgs) Handles btn_siguiente.Click
        BindingContext(table).Position += 1
        registro()
    End Sub

    Private Sub btn_ultimo_Click(sender As System.Object, e As System.EventArgs) Handles btn_ultimo.Click
        BindingContext(table).Position = BindingContext(table).Count - 1
        registro()
    End Sub
    '
    ''  FUNCIONES ''
    ''''''''''''''''
    Private Sub registro()
        Label11.Text = ((Me.BindingContext(table).Position + 1).ToString + " de ") + BindingContext(table).Count.ToString
    End Sub

    Public Sub validarWeb(ByVal direccion As String)
        'DESPUES DE IMPORTAR: System.Text.RegularExpressions
        Dim re As Regex = New Regex("^(www|http:\/\/)(.*\.(com$|net$|es$|org$|edu$|bo$))", RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(direccion)
        If m.Captures.Count = 0 Then
            ErrorProvider1.SetError(txt_webPro, "La dirección web especificada no es correcta.")
        Else
            ErrorProvider1.SetError(txt_webPro, Nothing)
            comprobarWeb(direccion)
        End If
    End Sub

    Public Sub comprobarWeb(ByVal direccion As String)
        Dim Peticion As System.Net.WebRequest
        Dim Respuesta As System.Net.HttpWebResponse
        Try
            Peticion = System.Net.WebRequest.Create("direccion")
            Respuesta = Peticion.GetResponse()
        Catch ex As System.Net.WebException
            If ex.Status = Net.WebExceptionStatus.NameResolutionFailure Then
                ErrorProvider1.SetError(txt_webPro, "El Sitio no existe")
            End If
        End Try
    End Sub


    Private Sub SplitContainer1_Panel2_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel2.Paint

    End Sub
End Class